package com.lastoffer2.offerview;

import java.util.List;

import com.example.lastoffer2.R;
import com.lastct.dao.Offer;
import com.lastoffer2.JSONConnector;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class OfferSectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_SECTION_NUMBER = "section_number";
		private Offer offer;
		public List<Offer> offers;
		private ProgressDialog pd;
		private TextView dummyTextView;

		public OfferSectionFragment() {			
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_list_offer, container, false);
			 dummyTextView = (TextView) rootView
					.findViewById(R.id.section_label);
			
			
			 this.pd = ProgressDialog.show(this.getActivity(), "Working..", "Downloading Data...", true, false);

		        // Start a new thread that will download all the data
		        new DownloadTask().execute("Any parameters my download task needs here");
			return rootView;
		}
		
		private class DownloadTask extends AsyncTask<String, Void, Object> {
	        protected Object doInBackground(String... args) {
	            Log.i("MyApp", "Background thread starting");
	            List<Offer> result = JSONConnector.getListOffers();
	            // This is where you would do all the work of downloading your data

	            return result;
	        }

	        protected void onPostExecute(Object result) {
	            // Pass the result data back to the main activity
	        	OfferSectionFragment.this.offers = (List<Offer>)result;
	        	
	            if (OfferSectionFragment.this.pd != null) {
	            	OfferSectionFragment.this.pd.dismiss();
	            }
	            
				offer = OfferSectionFragment.this.offers.get(getArguments().getInt(ARG_SECTION_NUMBER)-1);
				
				dummyTextView.setText(offer.description);
				dummyTextView.invalidate();
				
	        }
	   }    
	}